<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/9">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/40. shell 练习-下载文件/">shell 练习-下载文件</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>创建一个函数，能接受两个参数：</p>
<ol>
<li>第一个参数为URL，即可下载的文件；第二个参数为目录，即下载后保存的位置；</li>
<li>如果用户给的目录不存在，则提示用户是否创建；如果创建就继续执行，否则，函数返回一个51的错误值给调用脚本；</li>
<li>如果给的目录存在，则下载文件；下载命令执行结束后测试文件下载成功与否；如果成功，则&gt;返回0给调用脚本，否则，返回52给调用脚本；</li>
</ol>
<p>提示，在函数中返回错误值给调用脚本，使用return</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"></div><div class="line"><span class="keyword">if</span> [ ! -d <span class="variable">$2</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"please make directory"</span></div><div class="line">    <span class="built_in">exit</span> 51</div><div class="line"><span class="keyword">fi</span></div><div class="line"><span class="built_in">cd</span> <span class="variable">$2</span></div><div class="line">wget <span class="variable">$1</span></div><div class="line">n=`<span class="built_in">echo</span> $?`</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$n</span> -eq 0 ];<span class="keyword">then</span></div><div class="line">    <span class="built_in">exit</span> 0</div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">exit</span> 52</div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/4. shell 练习-监控机器存活状态/">shell 练习-监控机器存活状态</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>设计一个脚本，监控远程的一台机器(假设ip为123.23.11.21)的存活状态，当发现宕机时发一封邮件给你自己。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;提示：</p>
<ol>
<li>你可以使用ping命令   ping -c10 www.baidu.com</li>
<li>发邮件的命令是  echo “邮件内容” |mail -s “主题” abc@139.com</li>
<li>脚本可以搞成死循环，每隔30s检测一次</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"> <span class="comment">#!/bin/bash</span></div><div class="line"><span class="comment">#Remotemonitoring.Ifthenetworkisbroken,emailme~</span></div><div class="line"></div><div class="line">ping-c10www.baidu.com&gt;33.log</div><div class="line"></div><div class="line"><span class="keyword">while</span>[<span class="string">"1"</span>=<span class="string">"1"</span>]</div><div class="line"><span class="keyword">do</span></div><div class="line">t=$(awk<span class="string">'&#123;print$1&#125;'</span>33.log)</div><div class="line"><span class="keyword">if</span>[!-z$(<span class="variable">$t</span>)];<span class="keyword">then</span></div><div class="line"><span class="built_in">echo</span><span class="string">""</span></div><div class="line">sleep30</div><div class="line"><span class="keyword">else</span></div><div class="line"><span class="built_in">break</span>;</div><div class="line"><span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span><span class="string">"DiaoXianLe"</span>|mail<span class="_">-s</span><span class="string">"down"</span>abc@139.com</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/39. shell 练习-列出子目录/">shell 练习-列出子目录</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>脚本的功能：</p>
<p>脚本可以带参数也可以不带，参数可以有多个，每个参数必须是一个目录，脚本检查参数个数，若等于0，则列出当前目录本身；否则，显示每个参数包含的子目录。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">if</span> [ <span class="variable">$#</span> == 0 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    ls -ld `<span class="built_in">pwd</span>`</div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 <span class="variable">$#</span>`</div><div class="line">    <span class="keyword">do</span></div><div class="line">        a=<span class="variable">$i</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"ls <span class="variable">$&#123;!a&#125;</span>"</span></div><div class="line">        ls -l <span class="variable">$&#123;!a&#125;</span> |grep <span class="string">'^d'</span></div><div class="line">    <span class="keyword">done</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>标注： 可能会对${!a}有疑问，这里是一个特殊用法，在shell中，$1为第一个参数，$2为第二个参数，以此类推，那么这里的数字要是一个变量如何表示呢？比如n=3,我想取第三个参数，能否写成 $$n？ shell中是不支持的，那怎么办？ 就用脚本中的这种方法：  a=$n, echo ${!a} </p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/38. shell 练习-获取ip/">shell 练习-获取ip</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>提示用户输入网卡的名字，然后我们用脚本输出网卡的ip。 看似简单，但是需要考虑多个方面，比如我们输入的不符合网卡名字的规范，怎么应对。名字符合规范，但是根本就没有这个网卡有怎么应对。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">while</span> :</div><div class="line"><span class="keyword">do</span></div><div class="line">     <span class="built_in">read</span> -p <span class="string">"请输入网卡名: "</span> e</div><div class="line">     e1=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$e</span>"</span> | sed <span class="string">'s/[-0-9]//g'</span>`</div><div class="line">     e2=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$e</span>"</span> | sed <span class="string">'s/[a-zA-Z]//g'</span>`</div><div class="line">     <span class="keyword">if</span> [ -z <span class="variable">$e</span> ]</div><div class="line">     <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"你没有输入任何东西"</span></div><div class="line">        <span class="built_in">continue</span></div><div class="line">     <span class="keyword">elif</span> [ -z <span class="variable">$e1</span> ]</div><div class="line">     <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"不要输入纯数字在centos中网卡名是以eth开头后面加数字"</span></div><div class="line">        <span class="built_in">continue</span></div><div class="line">     <span class="keyword">elif</span> [ -z <span class="variable">$e2</span> ]</div><div class="line">     <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"不要输入纯字母在centos中网卡名是以eth开头后面加数字"</span></div><div class="line">        <span class="built_in">continue</span></div><div class="line">     <span class="keyword">else</span></div><div class="line">        <span class="built_in">break</span></div><div class="line">     <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="function"><span class="title">ip</span></span>() &#123;</div><div class="line">        ifconfig | grep -A1 <span class="string">"<span class="variable">$1</span> "</span> |tail -1 | awk <span class="string">'&#123;print $2&#125;'</span> | awk -F <span class="string">":"</span> <span class="string">'&#123;print $2&#125;'</span></div><div class="line">&#125;</div><div class="line">myip=`ip <span class="variable">$e</span>`</div><div class="line"><span class="keyword">if</span> [ -z <span class="variable">$myip</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"抱歉，没有这个网卡。"</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"你的网卡IP地址是<span class="variable">$myip</span>"</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/37. shell 练习-用户交互脚本/">shell 练习-用户交互脚本</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个脚本，执行后，打印一行提示“Please input a number:”，要求用户输入数值，然后打印出该数值，然后再次要求用户输入数值。直到用户输入”end”停止。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">while</span> :</div><div class="line"><span class="keyword">do</span></div><div class="line">        <span class="built_in">read</span> -p <span class="string">"Please input a number:(end for exit) "</span> n</div><div class="line">        num=` <span class="built_in">echo</span> <span class="variable">$n</span> |sed -r <span class="string">'s/[0-9]//g'</span>|wc -c `</div><div class="line">        <span class="keyword">if</span> [ <span class="variable">$n</span> == <span class="string">"end"</span> ]</div><div class="line">                <span class="keyword">then</span></div><div class="line">                        <span class="built_in">exit</span></div><div class="line">                <span class="keyword">elif</span> [ <span class="variable">$num</span> -ne 1  ]</div><div class="line">                        <span class="keyword">then</span></div><div class="line">                        <span class="built_in">echo</span> <span class="string">"what you input is not a number!Try again!"</span></div><div class="line">                <span class="keyword">else</span></div><div class="line">                        <span class="built_in">echo</span> <span class="string">"your input number is: <span class="variable">$n</span>"</span></div><div class="line">        <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/36. shell 练习-脚本传参/">shell 练习-脚本传参</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>使用传参的方法写个脚本，实现加减乘除的功能。例如：  sh  a.sh  1   2，这样会分别计算加、减、乘、除的结果。</p>
<p>要求：</p>
<ol>
<li>脚本需判断提供的两个数字必须为整数</li>
<li>当做减法或者除法时，需要判断哪个数字大</li>
<li>减法时需要用大的数字减小的数字</li>
<li>除法时需要用大的数字除以小的数字，并且结果需要保留两个小数点。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">if</span> [ <span class="variable">$#</span> -ne 2 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"The number of parameter is not 2, Please useage: ./<span class="variable">$0</span> 1 2"</span></div><div class="line">    <span class="built_in">exit</span> 1</div><div class="line"><span class="keyword">fi</span></div><div class="line"></div><div class="line">is_int()</div><div class="line">&#123;</div><div class="line">    <span class="keyword">if</span> <span class="built_in">echo</span> <span class="string">"<span class="variable">$1</span>"</span>|grep -q <span class="string">'[^0-9]'</span></div><div class="line">    <span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"<span class="variable">$1</span> is not integer number."</span></div><div class="line">    <span class="built_in">exit</span> 1</div><div class="line">    <span class="keyword">fi</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">max()</div><div class="line">&#123;</div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$1</span> -ge <span class="variable">$2</span> ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$1</span></div><div class="line">    <span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$2</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">min()</div><div class="line">&#123;</div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$1</span> -lt <span class="variable">$2</span> ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$1</span></div><div class="line">    <span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$2</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">sum()</div><div class="line">&#123;</div><div class="line">    <span class="built_in">echo</span> <span class="string">"<span class="variable">$1</span> + <span class="variable">$2</span> = $[<span class="variable">$1</span>+<span class="variable">$2</span>]"</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">minus()</div><div class="line">&#123;</div><div class="line">    big=`max <span class="variable">$1</span> <span class="variable">$2</span>`</div><div class="line">    small=`min <span class="variable">$1</span> <span class="variable">$2</span>`</div><div class="line">    <span class="built_in">echo</span> <span class="string">"<span class="variable">$big</span> - <span class="variable">$small</span> = $[<span class="variable">$big</span>-<span class="variable">$small</span>]"</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">mult()</div><div class="line">&#123;</div><div class="line">    <span class="built_in">echo</span> <span class="string">"<span class="variable">$1</span> * <span class="variable">$2</span> = $[<span class="variable">$1</span>*<span class="variable">$2</span>]"</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">div()</div><div class="line">&#123;</div><div class="line">    big=`max <span class="variable">$1</span> <span class="variable">$2</span>`</div><div class="line">    small=`min <span class="variable">$1</span> <span class="variable">$2</span>`</div><div class="line">    d=`<span class="built_in">echo</span> <span class="string">"scale =2; <span class="variable">$big</span> / <span class="variable">$small</span>"</span>|bc`</div><div class="line">    <span class="built_in">echo</span> <span class="string">"<span class="variable">$big</span> / <span class="variable">$small</span> = <span class="variable">$d</span>"</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">is_int <span class="variable">$1</span></div><div class="line">is_int <span class="variable">$2</span></div><div class="line">sum <span class="variable">$1</span> <span class="variable">$2</span></div><div class="line">minus <span class="variable">$1</span> <span class="variable">$2</span></div><div class="line">mult <span class="variable">$1</span> <span class="variable">$2</span></div><div class="line">div <span class="variable">$1</span> <span class="variable">$2</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/35. shell 练习-被3整除/">shell 练习-被3整除</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个脚本： 计算100以内所有能被3整除的正整数的和</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">sum=0</div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> &#123;1..100&#125;;<span class="keyword">do</span></div><div class="line">    <span class="keyword">if</span> [ $[<span class="variable">$i</span>%3] -eq 0 ];<span class="keyword">then</span></div><div class="line">    sum=$[<span class="variable">$i</span>+<span class="variable">$sum</span>]</div><div class="line">     <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"sum:<span class="variable">$sum</span>"</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/34. shell 练习-带选项的用户脚本/">shell 练习-带选项的用户脚本</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>要求如下：</p>
<ul>
<li>只支持三个选项 ‘–del’ ‘–add’ –help输入其他选项报错。</li>
<li>使用‘–add’需要验证用户名是否存在，存在则反馈存在。且不添加。 不存在则创建该用户，切添加与该用户名相同的密码。并且反馈。</li>
<li>使用‘–del’ 需要验证用户名是否存在，存在则删除用户及其家目录。不存在则反馈该用户不存在。</li>
<li>–help 选项反馈出使用方法</li>
<li>支持以，分隔   一次删除多个或者添加多个用户。</li>
<li>能用echo $?  检测脚本执行情况  成功删除或者添加为0,报错信息为其他数字。</li>
<li>能以，分割。一次性添加或者 删除多个用户。  例如 adddel.sh –add user1,user2,user3…….</li>
<li>不允许存在明显bug。</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#written by aming.</span></div><div class="line"><span class="keyword">if</span> [ <span class="variable">$#</span> -eq 0 -o <span class="variable">$#</span> -gt 2 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"use <span class="variable">$0</span> --add username or <span class="variable">$0</span> --del username or <span class="variable">$0</span> --help."</span></div><div class="line">    <span class="built_in">exit</span> 1</div><div class="line"><span class="keyword">fi</span></div><div class="line"><span class="keyword">case</span> <span class="variable">$1</span> <span class="keyword">in</span></div><div class="line">    --add)</div><div class="line">        n=0</div><div class="line">        <span class="keyword">for</span> u <span class="keyword">in</span> `<span class="built_in">echo</span> <span class="variable">$2</span>|sed <span class="string">'s/,/ /g'</span>`; <span class="keyword">do</span></div><div class="line">            <span class="keyword">if</span> awk -F: <span class="string">'&#123;print $1&#125;'</span> /etc/passwd |grep -qw <span class="string">"<span class="variable">$u</span>"</span></div><div class="line">            <span class="keyword">then</span></div><div class="line">                <span class="built_in">echo</span> <span class="string">"The user <span class="variable">$u</span> exist."</span></div><div class="line">            <span class="keyword">else</span></div><div class="line">                useradd <span class="variable">$u</span></div><div class="line">                <span class="built_in">echo</span> -e <span class="string">"<span class="variable">$u</span>\n<span class="variable">$u</span>"</span>|passwd <span class="variable">$u</span> &gt;/dev/null 2&gt;&amp;1</div><div class="line">                <span class="built_in">echo</span> <span class="string">"The user <span class="variable">$u</span> added successfully."</span></div><div class="line">                n=$[<span class="variable">$n</span>+1]</div><div class="line">            <span class="keyword">fi</span></div><div class="line">        <span class="keyword">done</span></div><div class="line">        <span class="keyword">if</span> [ <span class="variable">$n</span> -eq 0 ]; <span class="keyword">then</span></div><div class="line">            <span class="built_in">exit</span> 2</div><div class="line">        <span class="keyword">fi</span></div><div class="line">        ;;</div><div class="line">    --del)</div><div class="line">        n=0</div><div class="line">        <span class="keyword">for</span> u <span class="keyword">in</span> `<span class="built_in">echo</span> <span class="variable">$2</span>|sed <span class="string">'s/,/ /g'</span>`; <span class="keyword">do</span></div><div class="line">            <span class="keyword">if</span> awk -F: <span class="string">'&#123;print $1&#125;'</span> /etc/passwd|grep -qw <span class="string">"<span class="variable">$u</span>"</span></div><div class="line">            <span class="keyword">then</span></div><div class="line">                userdel -r <span class="variable">$u</span></div><div class="line">                <span class="built_in">echo</span> <span class="string">"The user <span class="variable">$u</span> deleted successfully."</span></div><div class="line">                n=$[<span class="variable">$n</span>+1]</div><div class="line">            <span class="keyword">else</span></div><div class="line">                <span class="built_in">echo</span> <span class="string">"The user <span class="variable">$u</span> not exist."</span></div><div class="line">            <span class="keyword">fi</span></div><div class="line">        <span class="keyword">done</span></div><div class="line">        <span class="keyword">if</span> [ <span class="variable">$n</span> -eq 0 ]; <span class="keyword">then</span></div><div class="line">                <span class="built_in">exit</span> 3</div><div class="line">        <span class="keyword">fi</span></div><div class="line">        ;;</div><div class="line">    --<span class="built_in">help</span>)</div><div class="line">        <span class="built_in">echo</span> -e <span class="string">"--add can add user,and the passwd is the same as username.     </span></div><div class="line"><span class="string">    It can add multiuser such as --add user1,user2,user3..."</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"--del cat delete user.It can delete user such as --del user1,user2,user3..."</span></div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"use <span class="variable">$0</span> --add username or <span class="variable">$0</span> --del username or <span class="variable">$0</span> --help."</span></div><div class="line">        <span class="built_in">exit</span> 1</div><div class="line">        ;;</div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/33. shell 练习-监控mysql服务/">shell 练习-监控mysql服务</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>假设，当前MySQL服务的root密码为123456，写脚本检测MySQL服务是否正常（比如，可以正常进入mysql执行show processlist），并检测一下当前的MySQL服务是主还是从，如果是从，请判断它的主从服务是否异常。如果是主，则不需要做什么。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">Mysql_c=<span class="string">"mysql -uroot -p123456"</span></div><div class="line"><span class="variable">$Mysql_c</span> -e <span class="string">"show processlist"</span> &gt;/tmp/mysql_pro.log 2&gt;/tmp/mysql_log.err</div><div class="line">n=`wc -l /tmp/mysql_log.err|awk <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$n</span> -gt 0 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"mysql service sth wrong."</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="variable">$Mysql_c</span> -e <span class="string">"show slave status\G"</span> &gt;/tmp/mysql_s.log</div><div class="line">    n1=`wc -l /tmp/mysql_s.log|awk <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$n1</span> -gt 0 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        y1=`grep <span class="string">'Slave_IO_Running:'</span> /tmp/mysql_s.log|awk -F : <span class="string">'&#123;print $2&#125;'</span>|sed <span class="string">'s/ //g'</span>`</div><div class="line">        y2=`grep <span class="string">'Slave_SQL_Running:'</span> /tmp/mysql_s.log|awk -F : <span class="string">'&#123;print $2&#125;'</span>|sed <span class="string">'s/ //g'</span>`</div><div class="line">        <span class="keyword">if</span> [ <span class="variable">$y1</span> == <span class="string">"Yes"</span> ] &amp;&amp; [ <span class="variable">$y2</span> == <span class="string">"Yes"</span> ]</div><div class="line">        <span class="keyword">then</span></div><div class="line">            <span class="built_in">echo</span> <span class="string">"slave status good."</span></div><div class="line">        <span class="keyword">else</span></div><div class="line">            <span class="built_in">echo</span> <span class="string">"slave down."</span></div><div class="line">        <span class="keyword">fi</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/32. shell 练习-判断是否开启web服务/">shell 练习-判断是否开启web服务</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个脚本判断你的Linux服务器里是否开启web服务？（监听80端口）如果开启了，请判断出跑的是什么服务，是httpd呢还是nginx又或者是其他的什么？</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">port=`netstat -lnp | grep 80`</div><div class="line"><span class="keyword">if</span> [ -z <span class="string">"port"</span> ]; <span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"not start service."</span>;</div><div class="line">    <span class="built_in">exit</span>;</div><div class="line"><span class="keyword">fi</span></div><div class="line">web_server=`<span class="built_in">echo</span> <span class="variable">$port</span> | awk -F<span class="string">'/'</span> <span class="string">'&#123;print $2&#125;'</span>|awk -F : <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line"><span class="keyword">case</span> <span class="variable">$web_server</span> <span class="keyword">in</span></div><div class="line">    httpd )     </div><div class="line">        <span class="built_in">echo</span> <span class="string">"apache server."</span> </div><div class="line">    ;;     </div><div class="line">    nginx )     </div><div class="line">         <span class="built_in">echo</span> <span class="string">"nginx server."</span> </div><div class="line">    ;;   </div><div class="line">    * ) </div><div class="line">         <span class="built_in">echo</span> <span class="string">"other server."</span>   </div><div class="line">    ;;   </div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/11">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
